package com.sino.extension;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;


/**
 * 
 * 	在bean初始化前调用
 *  Spring源码学习--BeanFactoryPostProcessor接口:https://blog.csdn.net/u013412772/article/details/80698996
 * BeanFactoryPostProcessor对bean配置元数据进行操作。
 * 也就是说，Spring IoC容器允许BeanFactoryPostProcessor读取配置元数据，并可能在容器实例化除BeanFactoryPostProcessor实例以外的任何bean之前对其进行更改。
 * @author amd
 *
 */
@Component
public class DemoBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
	private static final Logger log = LogManager.getLogger();
	

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		log.info("******调用了BeanFactoryPostProcessor");
		//转换为子类，因为父类没有添加beanDefintion对象的api
		DefaultListableBeanFactory defaultbeanFactory = (DefaultListableBeanFactory) beanFactory;
		
		//读取BeanDefinition
        String[] beanStr = defaultbeanFactory.getBeanDefinitionNames();
        for (String beanName : beanStr) {
                BeanDefinition beanDefinition = defaultbeanFactory.getBeanDefinition(beanName);
        }
        
        //添加beanDefinition对象，原本这个Y没有被spring扫描到
      	GenericBeanDefinition y= new GenericBeanDefinition();
  		defaultbeanFactory.registerBeanDefinition("y", y);

        
        
		log.info("------------------>"+beanFactory.getBeanDefinitionCount());
		
	}
}